﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IBatisNet.DataMapper;

namespace BatisNetStudy.Demo
{
    class OrderTest
    {
        public static void InserOrder()
        {
            var mapper = Mapper.Instance();

            var order = new Order
            {
                Customer = "段光伟"
            };

            var result = mapper.Insert("InsertOrder", order);

            Console.WriteLine(order);
            Console.WriteLine(result);
        }

        public static void UpdateOrder()
        {
            var mapper = Mapper.Instance();

            var order = new Order
            {
                Customer = "段光伟"
            };
            mapper.Insert("InsertOrder", order);
            Console.WriteLine(mapper.Update("UpdateOrder", order));
            Console.WriteLine(mapper.Delete("DeleteOrder", order));
        }

        public static void DeleteOrder()
        {
            var mapper = Mapper.Instance();

            var order = new Order
            {
                Customer = "段光伟"
            };
            mapper.Insert("InsertOrder", order);
            Console.WriteLine(mapper.Delete("DeleteOrder", order));
        }

        public static void LoadOrder()
        {
            var mapper = Mapper.Instance();

            var order1 = new Order { Customer = "段光伟" };
            var orderId = mapper.Insert("InsertOrder", order1);

            var order2 = mapper.QueryForObject<Order>("LoadOrder", orderId);
            var order3 = new Order();
            mapper.QueryForObject<Order>("LoadOrder", orderId, order3);

            Console.WriteLine(order1.Customer);
            Console.WriteLine(order2.Customer);
            Console.WriteLine(order3.Customer);
            Console.WriteLine(order1 == order2); // False
        }

        public static void FindOrdersByCustomer()
        {
            var mapper = Mapper.Instance();

            var orders = mapper.QueryForList<Order>("FindOrdersByCustomer", "段光伟", 3, 3);
            Console.WriteLine(orders.Count);
        }

        public static void FindOrdersWithRowDelegate()
        {
            var mapper = Mapper.Instance();

            var orders = mapper.QueryWithRowDelegate<Order>("FindOrdersByCustomer", "段光伟", (item, customer, items) =>
            {
                items.Add(item as Order);
            });
            Console.WriteLine(orders.Count);
        }

        public static void FindOrdersForDictionary()
        {
            var mapper = Mapper.Instance();

            var orders = mapper.QueryForDictionary<int, Order>("FindOrdersByCustomer", "段光伟", "Id");
            Console.WriteLine(orders.Count);
        }
    }
}
